import {
    newList
} from "../../api/new"
const globalData = getApp().globalData
Page({

    /**
     * 页面的初始数据
     */
    data: {
        triggered: false,
        globalData: globalData,
        listData: [],
        pageOption: {
            page: 1,
            pageSize: 5
        },
        listStatus: {
            isNoMore: false,
            total: 0
        },
        isEmpty: false,
        canRequest: true,
        tabs: [{
                id: 1,
                name: '国内',
                value: 'guonei'
            },
            {
                id: 2,
                name: '国际',
                value: 'guoji'
            },
            {
                id: 3,
                name: '军事',
                value: '123'
            },
        ],
        tabValue: 'guonei'
    },
    //切换tab
    tabChange(e) {
        this.setData({
            "pageOption.page": 1,
            tabValue: e.detail.value,
            listData: [],
            "listStatus.total": 0,
            isEmpty:false
        })
        this.getNewList()
    },
    //下拉刷新
    handleRefreshIng(val) {
        this.setData({
            triggered: true
        })
        setTimeout(() => {
            this.setData({
                triggered: false
            })
        }, 2000)
    },
    // 上拉触底事件
    loadMore() {
        console.log(this.data.listStatus)
        // 当前的无法在下拉刷新的时候
        if (!this.data.listStatus.isNoMore) {
            this.setData({
                "pageOption.page": this.data.pageOption.page += 1
            })
            this.getNewList()
        }

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getNewList()
    },
    getNewList() {
        // 防止重复触底触发的方法
        if (!this.data.canRequest) return
        this.setData({
            canRequest: false
        })
        newList(Object.assign({
            ...this.data.pageOption
        }, {
            type: this.data.tabValue
        })).then(res => {
            const {
                total,
                list,
                page,
                totalPage
            } = res
            if (total == 0) {
                this.setData({
                    isEmpty: true
                })
            }
            this.setData({
                listData: this.data.listData.concat(list),
                "listStatus.total": total,
                "listStatus.isNoMore": page == totalPage ? true : false,
                canRequest: true
            })
        })
    },
})